package com.nuyoah.service.impl;

import com.nuyoah.entity.ScoreRecord;
import com.nuyoah.mapper.ScoreRecordMapper;
import com.nuyoah.service.ScoreRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 存储学生的成绩信息 服务实现类
 * </p>
 *
 * @author xyx
 * @since 2024-06-03
 */
@Service
public class ScoreRecordServiceImpl extends ServiceImpl<ScoreRecordMapper, ScoreRecord> implements ScoreRecordService {

    @Autowired
    ScoreRecordMapper scoreRecordMapper;


    @Override
    public void addInfo(ScoreRecord scoreRecord) {

        scoreRecordMapper.insert(scoreRecord);
    }

    @Override
    public void updateInfo(ScoreRecord scoreRecord) {
        //先根据studentId查询该表是否有记录，如果有，则进行修改，如果没有，则无法修改
        ScoreRecord oldRecord = scoreRecordMapper.queryByStudentId(scoreRecord.getStudentId());
        if (oldRecord == null) {
            throw new RuntimeException("该学生还没有成绩信息，无法修改");
        }
        scoreRecordMapper.updateById(scoreRecord);
    }

    @Override
    public void deleteInfo(Integer id) {
        scoreRecordMapper.deleteByStudentId(id);
    }

    @Override
    public List<ScoreRecord> queryByUserId(Integer userId) {
        return scoreRecordMapper.queryByUserId(userId);
    }

    @Override
    public List<ScoreRecord> getAll() {
        return scoreRecordMapper.selectList(null);
    }

}
